package com.king.modules.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.king.common.constant.Constants;
import com.king.common.exception.AssertHelper;
import com.king.common.utils.uuid.SnowFlakeUtil;
import com.king.framework.base.service.impl.YyTreeServiceImpl;
import com.king.modules.system.domain.SysOrgEntity;
import com.king.modules.system.mapper.SysOrgMapper;
import com.king.modules.system.service.SysOrgService;

@Service
public class SysOrgServiceImpl extends YyTreeServiceImpl<SysOrgMapper, SysOrgEntity, Long> implements SysOrgService{

	
	@SuppressWarnings("unused")
	@Autowired
	private SysOrgMapper orgMapper;
	
	@Override
	public void beforeAdd(SysOrgEntity entity) throws Exception {
		entity.setId(SnowFlakeUtil.getInstance().nextId());
		if(entity.getParentid().longValue() == Constants.TREE_ORG_ROOTID.longValue()){//第二级的为集团
			entity.setGroupid(entity.getId());
		}else{
			SysOrgEntity parent = findByPK(entity.getParentid());
			AssertHelper.getInstance().checkNullException(parent, "不存在父节点:" + entity.getParentid());
			String nodePath = parent.getNodepath();
			//第二个节点作为集团id
			entity.setGroupid(Long.valueOf(nodePath.split(Constants.TREE_PATH_SPLIT)[1]));
		}
		super.beforeAdd(entity);
	}

}
